package com.pktechan.manage.service.impl;


import com.pktechan.manage.base.BaseMapper;
import com.pktechan.manage.base.BaseServiceImpl;
import com.pktechan.manage.controller.CommissionBalanceController;
import com.pktechan.manage.dao.CommissionBalanceMapper;
import com.pktechan.manage.entity.CommissionBalance;
import com.pktechan.manage.exception.CustomException;
import com.pktechan.manage.service.CommissionBalanceService;
import com.pktechan.manage.vo.CommissionBalanceVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service("commissionBalanceService")
public class CommissionBalanceServiceImpl extends BaseServiceImpl<CommissionBalance> implements CommissionBalanceService {

    private static final Logger logger = LoggerFactory.getLogger(CommissionBalanceServiceImpl.class);

    @Autowired
    private CommissionBalanceMapper commissionBalanceMapper;

    /**
     * 由业务类实现
     *
     * @return
     */
    @Override
    public BaseMapper<CommissionBalance> getBaseMapper() {
        return commissionBalanceMapper;
    }

    /**
     * 查询客户佣金管理列表
     * @param commissionBalanceVo
     * @return
     */
    @Override
    public List<CommissionBalanceVo> getCommissionBalanceVoList(CommissionBalanceVo commissionBalanceVo) {
        return commissionBalanceMapper.getCommissionBalanceVoList(commissionBalanceVo);
    }
    /**
     * 定时佣金解冻
     */
    @Override
    public void UnfreezingCommission() {
        //获取所有佣金持仓信息
        List<CommissionBalance> commissionBalanceList= commissionBalanceMapper.getAllNormal();
        //循环处理佣金信息
        for (CommissionBalance commissionBalance : commissionBalanceList) {
            BigDecimal availableBalance = BigDecimal.ZERO;//可用余额
            BigDecimal depositFreezeAmount = BigDecimal.ZERO;//入金冻结金额
            availableBalance = commissionBalance.getAvailableBalance().add(commissionBalance.getDepositFreezeAmount());
            commissionBalance.setAvailableBalance(availableBalance);
            commissionBalance.setDepositFreezeAmount(depositFreezeAmount);
            commissionBalanceMapper.update(commissionBalance);
        }

    }

    /**
     * 根据Id修改入金冻结金额并解冻
     * @param commissionBalance
     */
    @Override
    public void updateFreeiznfThwaingById(CommissionBalance commissionBalance) {
        CommissionBalance byId = (CommissionBalance) commissionBalanceMapper.getById(commissionBalance.getId());
        if(byId == null){
            logger.debug("未查询id="+commissionBalance.getId()+"到佣金持仓信息");
            throw new CustomException("未查询id="+commissionBalance.getId()+"到佣金持仓信息");
        }
        commissionBalance.setAvailableBalance(byId.getAvailableBalance().add(byId.getDepositFreezeAmount()));//可用余额 =可用余额+入金冻结金额
        commissionBalance.setDepositFreezeAmount(BigDecimal.ZERO);//入金冻结金额
        commissionBalanceMapper.update(commissionBalance);
    }
}
